#!/bin/sh
#
# Copyright (c) 2005-2010, Apple Computer, Inc. All rights reserved.
# 
# @APPLE_BSD_LICENSE_HEADER_START@
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer. 
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution. 
# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
#     its contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission. 
# 
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# @APPLE_BSD_LICENSE_HEADER_END@


###
### Given a list of filenames, this function will
### find the largest number after the '~' and return
### its value. Used to calculate a non-conflicting build number.
###
function GetBuildVersion() {
	local maxbuild="0"
	if [ $# -eq 0 ]; then
		echo ""
		return 0
	fi
	for X in $* ; do
		# Grab the numeric portion after the '~'
		build=$(echo $X | sed -e 's/^.*~\([0-9]*\)$/\1/')
		# If the regex fails, it somtimes prints the same line
		if [ "$build" != "$X" -a "$build" != "" ]; then
			# [ seems to things on the left of the -a, 
			# so do this in a different statement to avoid
			# errors about non-numeric values of $build
			if [ "$build" -gt "$maxbuild" ]; then
				maxbuild="$build"
			fi
		fi
	done
	echo $maxbuild
}

###
### Trap calls to ditto since it is only available on Mac OS X
### Warning: only supports the directory-to-directory form
function ditto() {
	local srcdir="$1"
	local dstdir="$2"
	if [ -x /usr/bin/ditto ]; then
		/usr/bin/ditto "$srcdir" "$dstdir"
	else
		tar c -C "$srcdir" . | tar xf - -C "$dstdir"
	fi
}

CURLARGS=${CURLARGS:-}
